/**
 * Created by black on 2017/1/8.
 */
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.HttpEntity;
import org.apache.http.util.*;
import org.apache.http.client.HttpClient;
import java.lang.*;
import java.sql.*;
import java.io.*;
import java.net.*;

public class Test {

    // JDBC driver name and database URL
    static final String DATABASE_URL="jdbc:mysql://localhost/homework_5?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    static final String USERNAME="root";
    static final String PASSWORD="bp19970306";

    public static void main(String[] args){
        try{
            for(int i=0;i<1000;i++) {
                HttpClient httpClient = HttpClients.createDefault();
                HttpGet req = new HttpGet("http://api.bilibili.com/archive_rank/getarchiverankbypartion?callback=jQuery17209095367502795187_1483854081239&type=jsonp&tid=17&pn="+i+"&_=1483854081470");
                req.addHeader("Accept", "*/*");
                req.addHeader("Accept-Encoding","gzip, deflate");
                req.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                req.addHeader("Content-Type","application/json; charset=UTF-8");
                req.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0");
                HttpResponse rep = httpClient.execute(req);
                HttpEntity repEntity = rep.getEntity();
                String content = EntityUtils.toString(repEntity);

                for(int j=0;j<20;j++) {
                    int n = content.indexOf("aid");
                    int m = content.indexOf("tid");
                    String aid = content.substring(n + 5, m - 2);

                    n=content.indexOf("author");
                    m=content.indexOf("face");
                    String author=content.substring(n+9,m-3);

                    n=content.indexOf("tid");
                    m=content.indexOf("tname");
                    String category=content.substring(n+5,m-2);

                    n=content.indexOf("coin");
                    m=content.indexOf("share");
                    String coin=content.substring(n+6,m-2);

                    n=content.indexOf("favorites");
                    m=content.indexOf("video_review");
                    String favorite=content.substring(n+11,m-2);

                    int start=content.indexOf("},{");
                    content=content.substring(start+2);

                    String sql=String.format("INSERT INTO fruit (aid,author,category,coin,favorite) " +
                            "VALUES(\"%s\",\"%s\",\"%s\",\"%s\",\"%s\");",aid,author,category,coin,favorite);

                    pushToDatabase(sql);

                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }


        // 下载视频
        downLoad();
    }

    public static void pushToDatabase(String sql){
        try{
            Connection connection=DriverManager.getConnection(
                    DATABASE_URL,USERNAME,PASSWORD);
            Statement statement=connection.createStatement();
            statement.executeUpdate(sql);
            connection.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public static void downLoad(){
        String remotePath  = "http://www.bilibilijj.com/Files/DownLoad/1413786.mp4/www.bilibilijj.com.mp4?mp3=true";
        try{
            URL url = new URL(remotePath);
            URLConnection conn = url.openConnection();
            int fileLength = conn.getContentLength();
            BufferedInputStream zipInputBuffer = new BufferedInputStream(conn.getInputStream());
            BufferedOutputStream fileOutBuffer = new BufferedOutputStream(new FileOutputStream(
                    new File("F:\\JavaDownLoad.mp4")));
            byte[] buffer = new byte[1024];
            int downLength = 0;
            int read = 0;
            while((read=zipInputBuffer.read(buffer))!=-1){
                fileOutBuffer.write(buffer, 0, read);
                downLength += read;
                System.out.println("总共"+(fileLength)/1024);
                System.out.println("-------剩余----------");
                System.out.println((fileLength-downLength)/1024);
            }
            System.out.println("下载完毕");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("下载异常");
        }
    }
}
